package com.wenhuitec.android.tang.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.wenhuitec.android.tang.R;
import com.wenhuitec.android.tang.entity.Orders;
import com.wenhuitec.android.tang.sysui.adapter.PickAdapter;
import com.wenhuitec.android.tang.sysui.model.PickModel;
import com.wenhuitec.android.tang.ui.dialog.OrdersDialog;

import java.util.List;


public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder> {


    private List<Orders> ordersList;
    private Context mContext;

    private OrdersOnClick mOrdersOnClick;
    private int selectPost = -1;

    public OrderAdapter(Context mContext, List<Orders> ordersList) {
        this.mContext = mContext;
        this.ordersList = ordersList;
    }

    public OrderAdapter(Context mContext, List<Orders> ordersList, OrdersOnClick ordersOnClick) {
        this.mContext = mContext;
        this.ordersList = ordersList;
        this.mOrdersOnClick = ordersOnClick;
    }

    public void setOrdersList(List<Orders> ordersList) {
        this.ordersList = ordersList;
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView orderTv;
        TextView numberTv;
        TextView priceTv;
        LinearLayout orderslay;

        public ViewHolder(View view) {
            super(view);
            orderTv = (TextView) view.findViewById(R.id.orderTv);
            numberTv = (TextView) view.findViewById(R.id.numberTv);
            priceTv = (TextView) view.findViewById(R.id.priceTv);
            orderslay = (LinearLayout) view.findViewById(R.id.orderslay);
        }
    }

    public interface OrdersOnClick {
        void userClick(Orders orders);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_orders, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.orderTv.setText(ordersList.get(position).getOrders());
        holder.numberTv.setText(ordersList.get(position).getNumber().stripTrailingZeros().toPlainString());
        holder.priceTv.setText(ordersList.get(position).getPrice().stripTrailingZeros().setScale(2).toPlainString());

        if (selectPost == position) {
            holder.orderslay.setBackgroundColor(Color.rgb(200, 200, 200));
        }else {
            holder.orderslay.setBackgroundColor(Color.rgb(255, 255, 255));
        }
        holder.orderslay.setOnClickListener(v -> {
            selectPost = position;
            if (mOrdersOnClick != null) {
                mOrdersOnClick.userClick(ordersList.get(position));
            }
            notifyDataSetChanged();
//            OrdersDialog.startAct(mContext, JSONObject.toJSONString(ordersList.get(position).getOrderProductModel()));
        });
    }

    @Override
    public int getItemCount() {
        return ordersList.size();
    }


}
